package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.oocl.dao.CustomerDao;
import com.oocl.entity.Customer;
import com.oocl.utils.DbUtils;

public class CustomerDaoImpl implements CustomerDao {

	public int addCustomer(Customer customer) {
		int result = 0;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			String sql = "insert into customer(cid,uname,account,password) values(customerseq.nextval,?,?,?)";
			connection = DbUtils.connect();
			connection.setAutoCommit(false);
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, customer.getUname());
			preparedStatement.setString(2, customer.getAccount());
			preparedStatement.setString(3, customer.getPassword());
			result = preparedStatement.executeUpdate();
			connection.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DbUtils.free(connection, preparedStatement, null);
		}
		return result;
	}

	public int deleteCustomer(int cid) {
		int result = 0;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			String sql = "delete from customer where cid=?";
			connection = DbUtils.connect();
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, cid);
			result = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.free(connection, preparedStatement, null);
		}
		return result;
	}

	public List<Customer> findAllCustomers() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		List<Customer> customers = new ArrayList<Customer>();
		ResultSet resultSet = null;
		try {
			String sql = "select * from customer";
			connection = DbUtils.connect();
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();

			while (resultSet.next()) {
				Customer customer = new Customer();
				customer.setCid(resultSet.getInt("cid"));
				customer.setUname(resultSet.getString("uname"));
				customer.setAccount(resultSet.getString("account"));
				customer.setPassword(resultSet.getString("password"));
				customers.add(customer);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.free(connection, preparedStatement, resultSet);
		}
		return customers;
	}

	public Customer findCustomer(String account, String password) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Customer customer = null;
		try {
			String sql = "select * from customer where account=? and password=?";
			connection = DbUtils.connect();
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, account);
			preparedStatement.setString(2, password);
			resultSet = preparedStatement.executeQuery();			
			if(resultSet.next()) {
				customer = new Customer();
				customer.setCid(resultSet.getInt("cid"));
				customer.setUname(resultSet.getString("uname"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.free(connection, preparedStatement, resultSet);
		}
		return customer;
	}

}
